import { type baseListInfo } from '@/page/types/base/baseList'
import { type pageInfo } from '@/page/types/base/baseRequestPage'
import { userStore } from '@/store/user'
import { type selectDeptInfo } from '@/page/types/admin/dept'
import { isMobile } from '@/util/checkStr'

export interface userDataInfo {
  id: string | null
  loginName: string
  nickName: string
  code: string
  type: string
  phone: string
  email: string
  sex: string
  avatar: string
  createTime: string
  updateTime: string
  stop: boolean | null
  company: baseListInfo | null
  dept: baseListInfo | null
  roleList: baseListInfo[]
  roleIdList: number[]
  job: baseListInfo | null
  companyId: string | null
  deptId: string | null
  jobId: string | null
}

export interface userInfo {
  id: string | undefined
  loginName: string
  nickName: string
  code: string
  type: { name: string | null; desc: string | null }
  phone: string
  email: string
  companyId: string | null
  deptId: string | null
  jobId: string | null
  sex: string
  avatar: string
  createTime: string
  updateTime: string
  stop: boolean | null
  roleList: number[]
}

export interface userS extends pageInfo {
  loginName: string | null
  phone: string | null
  stop: boolean | null
}

export interface restPwInfo {
  oldPassword: string
  password: string
  conformPassword: string
}

export interface myUserData {
  id: string | null
  loginName: string
  nickName: string
  phone: string
  email: string
  avatar: string
}

/**
 * 个人信息修改
 */
export class userInfoClass {
  userStore = userStore().userInfo
  userSelectList = [] as baseListInfo[]
  activeName = 'userInfo'
  editUser: myUserData = {
    id: '',
    loginName: '',
    nickName: '',
    phone: '',
    email: '',
    avatar: ''
  }

  restPwdata: restPwInfo = {
    oldPassword: '',
    password: '',
    conformPassword: ''
  }

  detail = {
    compamyName: '',
    deptList: '',
    jobList: '',
    roleList: ''
  }
  userRole = reactive({
    loginName: [{ required: true, message: '用户名不能为空', trigger: 'blur' }]
  })

  validatePass2 = (rule: any, value: any, callback: any) => {
    if (value === '') {
      callback(new Error('请再次输入密码'))
    } else if (value !== this.restPwdata.password) {
      callback(new Error('两次输入密码不一致!'))
    } else {
      callback()
    }
  }

  pwRules = reactive({
    oldPassword: [{ required: true, message: '老密码不能为空', trigger: 'blur' }],
    password: [{ required: true, message: '密码不能为空', trigger: 'blur' }],
    conformPassword: [{ validator: this.validatePass2, trigger: 'blur' }]
  })
}
/**
 * token 用户信息
 */
export class tokenUserClass {
  userData: userDataInfo = {
    id: '',
    loginName: '',
    nickName: '',
    code: '',
    type: '',
    phone: '',
    email: '',
    sex: '',
    avatar: '',
    createTime: '',
    updateTime: '',
    stop: null,
    company: null,
    dept: null,
    roleList: [],
    roleIdList: [],
    job: null,
    companyId: null,
    deptId: null,
    jobId: null
  }
}
/**
 * 用户列表管理
 */
export class operateUserClass {
  selectCompanyData = [] as baseListInfo[]
  selectDeptTree = [] as selectDeptInfo[]
  jobSelect = [] as baseListInfo[]
  roleAll = [] as baseListInfo[]
  tableData = [] as userInfo[]
  dialogFormVisible = false
  userS: userS = {
    loginName: null,
    page: 0,
    phone: null,
    size: 10,
    stop: null,
    total: 0
  }
  editInfoData: userDataInfo = {
    avatar: '',
    code: '',
    company: null,
    companyId: null,
    createTime: '',
    dept: null,
    deptId: null,
    email: '',
    id: null,
    job: null,
    jobId: null,
    loginName: '',
    nickName: '',
    phone: '',
    roleIdList: [],
    roleList: [],
    sex: '',
    stop: null,
    type: '',
    updateTime: ''
  }
  options = [
    {
      value: true,
      label: '正常'
    },
    {
      value: false,
      label: '停用'
    }
  ]
  addRules = {
    loginName: [
      { required: true, message: '用户名不能为空' },
      { min: 3, max: 50, message: '长度超过3小于50', trigger: 'blur' }
    ],
    phone: [{ required: true, validator: isMobile, trigger: 'blur' }]
  }
  props = {
    label: 'deptName',
    value: 'id',
    children: 'children',
    checkStrictly: true,
    emitPath: false
  }
}
